//
//  GoodDetailViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/8/30.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "DetailBannerCell.h"
#import "DetailCountDownCell.h"
#import "DetailPriceCell.h"
#import "DetailSupplierCell.h"
#import "DetailBrandCell.h"
#import "DetailOtherStoreCell.h"
#import "DetailImageAndTextCell.h"
#import "DetailGoodInfoCell.h"
#import "DetailGuaranteeCell.h"
#import "DetailOfferRecordCell.h"
#import "DetailWebCell.h"

#import "DetailActionCell.h"
#import "HomeDistanceCell.h"
#import "DetailImageCell.h"

#import "SubscriptButton.h"

#import "OYCountDownManager.h"

#import "DetailGoodInfoModel.h"

#import "GoodDetailViewController.h"
#import "PaingSocketViewController.h"
#import "MallPaiSocketViewController.h"

#import "GoodDetailViewController.h"

NSString *const k_ppGoodDetailItemSource = @"k_ppGoodDetailItemSource";

@interface GoodDetailViewController ()<UITableViewDelegate, UITableViewDataSource,WKNavigationDelegate>

@property (strong, nonatomic) UITableView *tableview;

@property (nonatomic, assign) CGFloat contextCellHeight;//图文详情web高度

@property (strong, nonatomic) UIView *bottomView;
@property (strong, nonatomic) UIButton *goHomeBtn;

@property (nonatomic, strong) DetailGoodInfoModel *detailModel;

@property (nonatomic, strong) SubscriptButton *incomeItem;
@property (nonatomic, strong) SubscriptButton *mineItem;
@property (nonatomic, strong) SubscriptButton *collectItem;

@property (nonatomic, strong) UIButton *paiBtn;
@property (nonatomic, strong) UIButton *backBtn;

@property (nonatomic, strong) NSMutableDictionary *heightDic;

@property (nonatomic, strong) NSMutableArray *firstSectionArray;

@property (nonatomic, assign) BOOL isLoadFinish;

@end

@implementation GoodDetailViewController

- (void)dealloc {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
    self.isLoadFinish = NO;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.bottomView pp_clipCorners:UIRectCornerTopLeft radius:1 border:0.5 color:PP_Default_Color_Gray_F1F1F1];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self.navigationController changeTransparentNavigation];
    
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        self.tableview.contentInset = UIEdgeInsetsZero;
    }
    
    [self request_ShopDetailData];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    [self.navigationController changeNormalNavigation];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:YES];
}

- (void)selectCollectItem {
    
    self.detailModel.isFollow = YES;
    [self.collectItem.topButton setImage:[UIImage imageNamed:@"collect_sel"] forState:UIControlStateNormal];
}

- (void)request_CollectGoodsDetail {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.goodsID forKey:@"id"];
    [dic setValue:@"1" forKey:@"status"];
    [dic setValue:@(self.type) forKey:@"goodDetailType"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_AUCTION_FOLLOW];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        //返回data为字典
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * code = [data valueForKey:@"code"];
            //请求成功
            if ([code isEqualToString:@"200"]) {
                
                [self selectCollectItem];
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            } else {

                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
            //刷新
        } else {
            
            //返回为空
            [SVProgressHUD showErrorWithStatus:@"返回为空"];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
    
}

- (void)request_ShopDetailData {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    if (self.isFromOrder) {
        [dic setValue:@"1" forKey:@"fromWhere"];
    } else {
        [dic setValue:@"0" forKey:@"fromWhere"];
    }
    
    [dic setValue:@(self.type) forKey:@"goodDetailType"];
    [dic setValue:self.goodsID forKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_SHOP_DETAIL];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        //返回data为字典
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * code = [data valueForKey:@"code"];
            //请求成功
            if ([code isEqualToString:@"200"]) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    [self.detailModel.itemImageArray removeAllObjects];
                    [self.detailModel.itemImageArray addObject:[resultDic valueForKey:@"coverImgPath"]];
                    self.detailModel.itemGoodName = [resultDic valueForKey:@"goodsName"];
                    self.detailModel.itemGoodID = [resultDic valueForKey:@"id"];
                    
                    self.detailModel.itemStartPrice = [[resultDic valueForKey:@"goodsStartingPrice"] doubleValue];//起拍价
                    self.detailModel.itemTopPrice = [[resultDic valueForKey:@"goodsCappedPrice"] doubleValue];//上限价格
                    self.detailModel.itemAddPriceRange = [[resultDic valueForKey:@"goodsPriceIncreaseRange"] doubleValue];//加价幅度
                    self.detailModel.itemMarketPrice = [[resultDic valueForKey:@"goodsMarketPrice"] doubleValue];//市场价
                    
                    self.detailModel.itemFreightPrice = [[resultDic valueForKey:@"goodsBottomPrice"] doubleValue];
                    
                    NSString *webUrl = [resultDic valueForKey:@"websiteUrl"];
                    if (![webUrl isKindOfClass:[NSString class]]) {
                        webUrl = [NSString stringWithFormat:@"%@",webUrl];
                    }
                    self.detailModel.itemStoreUrl = webUrl;
                    
                    self.detailModel.itemBrandName = [resultDic valueForKey:@"brandName"];
                    self.detailModel.itemSupplierName = [resultDic valueForKey:@"supplierName"];
                    self.detailModel.itemStartTime = [[resultDic valueForKey:@"startTime"] doubleValue];
                    self.detailModel.itemEndTime = [[resultDic valueForKey:@"endTime"] doubleValue];
                    
                    self.detailModel.textAndImageArray = @[].mutableCopy;
                    NSString *htmlStr = [resultDic valueForKey:@"description"];
                    self.detailModel.itemHtmlStr = htmlStr;
                    
                    NSString *goodDetailType = [NSString stringWithFormat:@"%@",[resultDic valueForKey:@"goodDetailType"]];
                    self.detailModel.goodDetailType = goodDetailType;
                    
                    self.detailModel.hasCoupon = [[resultDic valueForKey:@"hasCoupon"] boolValue];
                    self.detailModel.isFollow = [[resultDic valueForKey:@"isFollow"] boolValue];
                    
                    if (self.detailModel.isFollow) {

                        [self selectCollectItem];

                    }

                }
                
                //状态页隐藏
                [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
                
                [self.tableview reloadData];
                
            } else {
                
                //检查到未登录
                if ([code isEqualToString:@"401"]) {
                    [[TTUserModel sharedObject] initialization];
                    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                        
                        if (isLogin) {
                            [self request_ShopDetailData];
                        } else {
                            [self.navigationController popToRootViewControllerAnimated:NO];
                        }
                    }];
                    
                } else {
                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"返回" didPressButton:^{
                        
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                    
                }
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
            //刷新
        } else {
            
            //返回为空
            [SVProgressHUD showErrorWithStatus:@"返回为空"];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismissWithDelay:0.5];
        
        WEAKSELF
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
            [weakSelf request_ShopDetailData];
        }];
        
    }];
}

- (void)goPai {
    
    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
        if (isLogin) {
            
            if (self.detailModel.itemGoodID) {
                
                if (self.type == PPMallPaiGoodsDetail || self.type == PPMallPaiMustGetGoodsDetail) {
                    MallPaiSocketViewController *VC = [[MallPaiSocketViewController alloc] init];
                    
                    if ([self.detailModel.goodDetailType isEqualToString:@"30"]) {
                        
                        if (self.detailModel.hasCoupon) {
                            VC.type = PPPaiDetailType_PaiMustGet;
                            VC.goodsInfoModel = self.detailModel;
                            [self.navigationController pushViewController:VC animated:YES];
                        } else {
                            [self alert:@"您尚无一拍必得专场的机会，请去邀请好友获取机会"];
                        }
                        
                    } else {
                        
                        VC.type = PPPaiDetailType_MallPai;
                        VC.goodsInfoModel = self.detailModel;
                        [self.navigationController pushViewController:VC animated:YES];
                    }

                } else {
                    PaingSocketViewController *VC = [[PaingSocketViewController alloc] init];
                    VC.type = PPPaiDetailType_NormalPai;
                    VC.goodsInfoModel = self.detailModel;
                    [self.navigationController pushViewController:VC animated:YES];

                }
            }

        } else {
            return;
        }
    }];
}

- (void)initView {
    
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = UIColorHex(0xf5f5f5);
    
    _contextCellHeight = CGFLOAT_MIN;
        
    if (self.isFromOrder) {
        self.bottomView.hidden = YES;
    }
    
    [self.view addSubview:self.tableview];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.backBtn];
    
    [self.bottomView addSubview:self.paiBtn];
    [self.bottomView addSubview:self.incomeItem];
    [self.bottomView addSubview:self.mineItem];
    
    CGFloat bottomHeight = 0;
    if (!self.isFromOrder) {
        bottomHeight = 50 + kBottomSafeHeight;
    } else {
        bottomHeight = 0 ;
    }
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(bottomHeight );
    }];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(kStatusBarHeight + 10);
    }];
    
    [self.paiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(50);
    }];
    
    [self.incomeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.paiBtn);
        make.left.mas_equalTo(30);
        make.width.height.mas_equalTo(40);
    }];
    
    [self.mineItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.paiBtn);
        make.left.mas_equalTo(self.incomeItem.mas_right).offset(30);
        make.width.height.mas_equalTo(40);
    }];
    
    
    if (self.type != PPPaiingGoodsDetail) {
        [self.bottomView addSubview:self.collectItem];
        [self.collectItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.paiBtn);
            make.left.mas_equalTo(self.mineItem.mas_right).offset(30);
            make.width.height.mas_equalTo(40);
        }];
    }
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goMine {
    [self.navigationController changeNormalNavigation];
    [self.navigationController popToRootViewControllerAnimated:NO];
    [PPTabBarController setCurrentIndex:PPTabbarIndexTypeMine];
}

- (void)goIncome {
    [self.navigationController changeNormalNavigation];
    [self.navigationController popToRootViewControllerAnimated:NO];
    [PPTabBarController setCurrentIndex:PPTabbarIndexTypeIncome];
}

- (void)collectGoods {
    
    if (self.goodsID.length && !self.detailModel.isFollow) {
        [self request_CollectGoodsDetail];
    }
}

#pragma mark tableview delegate && datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case PP_GOODDETAIL_SECTION_FIRST:{
            return 5;
        } break;
        case PP_GOODDETAIL_SECTION_SECOND:{
            
            switch (self.type) {
                case PPPaiingGoodsDetail:
                    return 0;
                    break;
                case PPMallPaiGoodsDetail:{
                    return 4;
                } break;
                  case PPMallPaiMustGetGoodsDetail:{

                      return 4;
                  } break;
                default:{
                    return 0;
                } break;
            }

        } break;
        case PP_GOODDETAIL_SECTION_THIRD:{
            return 4;
        } break;
        case PP_GOODDETAIL_SECTION_FOURTH:{
            return 2;

        } break;
            
        default:{
            return 0;
        } break;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == PP_GOODDETAIL_SECTION_FIRST) {              //第一分组
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_ONE_BANNER:{
                return  TTScreenWidth;

            } break;
            case PP_GOODDETAIL_SECTION_ONE_COUNTDOWN:{
                
                return CGFLOAT_MIN;
                
            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSINFO:{
                return 70;

            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSPRICE:{
                
                if (self.isFromOrder) {
                    return CGFLOAT_MIN;
                } else {
                    
                    switch (self.type) {
                        case PPPaiingGoodsDetail:{
                            return 70;
                        } break;
                        case PPMallPaiGoodsDetail:{
                            return 70;
                        } break;
                        case PPMallPaiMustGetGoodsDetail:{
                            return 70;
                        } break;
                        default:{
                            return CGFLOAT_MIN;
                        } break;
                    }
                }

            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSGUARANTEE:{
                
                if (self.isFromOrder) {
                    return CGFLOAT_MIN;
                } else {
                    return 40;
                }
                
            } break;
                
            default:{
                return 10;

            } break;
        }
        
    } else if (indexPath.section == PP_GOODDETAIL_SECTION_SECOND) {
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_TWO_RECORD:{
//                return 45;
                return CGFLOAT_MIN;
            } break;
            default:{
//                return 40;
                return CGFLOAT_MIN;

            } break;
        }
        
    }  else if (indexPath.section == PP_GOODDETAIL_SECTION_THIRD) {
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_THREE_OTHERSTORE:{
                
                if ([self.detailModel.itemStoreUrl isKindOfClass:[NSString class]] &&
                    [self.detailModel.itemStoreUrl hasPrefix:@"http"] ) {
                    
                    return 45;
                } else {
                    return CGFLOAT_MIN;
                }
            } break;
            case PP_GOODDETAIL_SECTION_THREE_BRAND:{
                
                return 45;
            } break;
            case PP_GOODDETAIL_SECTION_THREE_SUPPLIER:{
                
                return 45;
            } break;
            case PP_GOODDETAIL_SECTION_THREE_AUCTIONRULE:{
                return 45;
            } break;
            default:{
                return 45;
            } break;
        }

    } else {                                   //间距的cell HomeDistanceCellID
        
        if (indexPath.row == PP_GOODDETAIL_SECTION_FOUR_HTMLHEAD) {
            
            return 45;
        } else if (indexPath.row == PP_GOODDETAIL_SECTION_FOUR_HTMLPAGE){

            return _contextCellHeight + 20;
        } else {
            return CGFLOAT_MIN;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == PP_GOODDETAIL_SECTION_FIRST) {              //第一分组
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_ONE_BANNER:{
                
                DetailBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailBannerCellID" forIndexPath:indexPath];
                [cell configBannerCellWithImageArray:self.detailModel.itemImageArray];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
            case PP_GOODDETAIL_SECTION_ONE_COUNTDOWN:{

                HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
                
//                DetailCountDownCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCountDownCellID" forIndexPath:indexPath];
//                [cell fillCellWithModel:self.detailModel];
//                cell.selectionStyle =UITableViewCellSelectionStyleNone;
//
//                return cell;
            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSINFO:{
                
                DetailGoodInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailGoodInfoCellID" forIndexPath:indexPath];
                [cell fillCellWithModel:self.detailModel];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSPRICE:{
                
                if (self.isFromOrder) {
                    
                    HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    return cell;
                } else {
                    
                    switch (self.type) {
                        case PPPaiingGoodsDetail:{
                            DetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailPriceCellID" forIndexPath:indexPath];
                            [cell fillCellWithModel:self.detailModel];
                            cell.selectionStyle =UITableViewCellSelectionStyleNone;
                            
                            return cell;
                        } break;
                        case PPMallPaiGoodsDetail:{
                            //暂时改回去
                            DetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailPriceCellID" forIndexPath:indexPath];
                            [cell fillCellWithModel:self.detailModel];
                            cell.selectionStyle =UITableViewCellSelectionStyleNone;
                            
                            return cell;
                        } break;
                            case PPMallPaiMustGetGoodsDetail:{
                                //暂时改回去
                                DetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailPriceCellID" forIndexPath:indexPath];
                                [cell fillCellWithModel:self.detailModel];
                                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                                
                                return cell;
                            } break;
                            
                        default:{
                            HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
                            cell.selectionStyle =UITableViewCellSelectionStyleNone;
                            
                            return cell;
                        } break;
                    }
                }
                

            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSGUARANTEE:{
                
                DetailGuaranteeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailGuaranteeCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
                
            default:{
                
                HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
        }
        
    } else if (indexPath.section == PP_GOODDETAIL_SECTION_SECOND) {
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_TWO_RECORD:{

                DetailActionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailActionCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                cell.titleLab.text = @"参拍记录";
                return cell;
            } break;
            default:{

                DetailOfferRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailOfferRecordCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                return cell;
            } break;
        }
        
    } else if (indexPath.section == PP_GOODDETAIL_SECTION_THIRD) {

        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_THREE_OTHERSTORE:{
                DetailOtherStoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailOtherStoreCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
            case PP_GOODDETAIL_SECTION_THREE_BRAND:{
                DetailBrandCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailBrandCellID" forIndexPath:indexPath];
                [cell fillCellWithModel:self.detailModel];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
            case PP_GOODDETAIL_SECTION_THREE_SUPPLIER:{
                DetailSupplierCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailSupplierCellID" forIndexPath:indexPath];
                [cell fillCellWithModel:self.detailModel];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
            case PP_GOODDETAIL_SECTION_THREE_AUCTIONRULE:{
                DetailActionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailActionCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
            default:{
                HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
                
                return cell;
            } break;
        }
        
    } else {                                   //间距的cell HomeDistanceCellID
        
        if (indexPath.row == PP_GOODDETAIL_SECTION_FOUR_HTMLHEAD) {
            DetailImageAndTextCell *cell;
            if (!cell) {
                cell = [tableView dequeueReusableCellWithIdentifier:@"DetailImageAndTextCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
            }
            return cell;
            
        } else if (indexPath.row == PP_GOODDETAIL_SECTION_FOUR_HTMLPAGE){
            
            DetailWebCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailWebCellID" forIndexPath:indexPath];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.navigationDelegate = self;

            if (!self.isLoadFinish && self.detailModel.itemHtmlStr) {
                [cell reloadHtmlWithStr:self.detailModel.itemHtmlStr];
            }
            return cell;

        } else {
                HomeDistanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeDistanceCellID" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;

                return cell;
        }
    }
    
    return nil;
}

#pragma mark ---------- configureCell ---------- 根据图片高度来计算cell高度 ----------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == PP_GOODDETAIL_SECTION_FIRST) {              //第一分组
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_ONE_BANNER:{
                
            } break;
            case PP_GOODDETAIL_SECTION_ONE_COUNTDOWN:{
                
            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSINFO:{
                
            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSPRICE:{
                
            } break;
            case PP_GOODDETAIL_SECTION_ONE_GOODSGUARANTEE:{
                
            } break;
                
            default:{
                
            } break;
        }
        
    } else if (indexPath.section == PP_GOODDETAIL_SECTION_SECOND) {
        
    } else if (indexPath.section == PP_GOODDETAIL_SECTION_THIRD) {
        
        switch (indexPath.row) {
            case PP_GOODDETAIL_SECTION_THREE_OTHERSTORE:{

                PP_jumpModel *model = [[PP_jumpModel alloc] init];
                model.type = CommentClickType_HtmlPage;
                model.ID = self.detailModel.itemStoreUrl;
                
                if ([model.ID isKindOfClass:[NSString class]] && [model.ID hasPrefix:@"http"] ) {
                    [[TTJumpManager share] pushViewControllerWithModel:model];
                } else {
                    [self alert:[NSString stringWithFormat:@"不规范的地址：“%@”",model.ID]];
                }
            } break;
            case PP_GOODDETAIL_SECTION_THREE_BRAND:{

            } break;
            case PP_GOODDETAIL_SECTION_THREE_SUPPLIER:{

            } break;
            case PP_GOODDETAIL_SECTION_THREE_AUCTIONRULE:{

                PP_jumpModel *model = [[PP_jumpModel alloc] init];
                model.type = CommentClickType_HtmlPage;
                model.ID = [NSString stringWithFormat:@"%@%@",TT_Global_H5_Domain,TT_HTML_AUCTION_RULES];
                [[TTJumpManager share] pushViewControllerWithModel:model];
                
            } break;
            default:{

            } break;
        }
        
    } else {                                   //图文详情头    DetailImageAndTextCell
        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headview = [[UIView alloc]init];
    headview.backgroundColor = UIColorHex(0xf5f5f5);
    return headview;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    switch (section) {
        case PP_GOODDETAIL_SECTION_FIRST:{
            return CGFLOAT_MIN;
        } break;
        case PP_GOODDETAIL_SECTION_SECOND:{
            
            if (self.type == PPMallPaiGoodsDetail) {
                return 10;
            } else {
                return CGFLOAT_MIN;
            }

        } break;
        case PP_GOODDETAIL_SECTION_THIRD:{
            return 5;
        } break;
        case PP_GOODDETAIL_SECTION_FOURTH:{
            return 5;
        } break;
            
        default:{
            return CGFLOAT_MIN;
        } break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor brownColor];

    switch (section) {
        case PP_GOODDETAIL_SECTION_FIRST:{
            return view;
        } break;
        case PP_GOODDETAIL_SECTION_SECOND:{
            return view;
        } break;
        case PP_GOODDETAIL_SECTION_THIRD:{
            return view;
        } break;
        case PP_GOODDETAIL_SECTION_FOURTH:{
            return view;
        } break;
            
        default:{
            return view;
        } break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    switch (section) {
        case PP_GOODDETAIL_SECTION_FIRST:{
            return CGFLOAT_MIN;
        } break;
        case PP_GOODDETAIL_SECTION_SECOND:{
            return CGFLOAT_MIN;
        } break;
        case PP_GOODDETAIL_SECTION_THIRD:{
            return CGFLOAT_MIN;
        } break;
        case PP_GOODDETAIL_SECTION_FOURTH:{
            return CGFLOAT_MIN;
        } break;
            
        default:{
            return CGFLOAT_MIN;
        } break;
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    
    if (webView.tag != 100) {
        
        WEAKSELF
        [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
            
            weakSelf.contextCellHeight = [obj floatValue];
            NSLog(@"%lf",weakSelf.contextCellHeight);
            [webView sizeThatFits:CGSizeMake(weakSelf.view.bounds.size.width, weakSelf.contextCellHeight)];
            if (!weakSelf.isLoadFinish) {
                [weakSelf.tableview reloadData];
                weakSelf.isLoadFinish = YES;
            }
        }];
    }
}

- (SubscriptButton *)mineItem {
    if (!_mineItem) {
        _mineItem = [[SubscriptButton alloc] initWithSize:CGSizeMake(40, 40)];
        [_mineItem.topButton setTitle:@"我的" forState:UIControlStateNormal];
        [_mineItem.topButton imageSetting:@"mine_small" selectedImage:@"mine_small"];
        //        [_mineItem setCountNum:0];
        _mineItem.countLabel.hidden = YES;
        _mineItem.userInteractionEnabled = YES;
        _mineItem.topButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [_mineItem.topButton addTarget:self action:@selector(goMine) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mineItem;
}

- (SubscriptButton *)incomeItem {
    if (!_incomeItem) {
        _incomeItem = [[SubscriptButton alloc] initWithSize:CGSizeMake(40, 40)];
        [_incomeItem.topButton setTitle:@"收益" forState:UIControlStateNormal];
        [_incomeItem.topButton imageSetting:@"income_small" selectedImage:@"income_small"];
        //        [_incomeItem setCountNum:0];
        _incomeItem.countLabel.hidden = YES;
        _incomeItem.userInteractionEnabled = YES;
        _incomeItem.topButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [_incomeItem.topButton addTarget:self action:@selector(goIncome) forControlEvents:UIControlEventTouchUpInside];
    }
    return _incomeItem;
    
}

- (SubscriptButton *)collectItem {
    if (!_collectItem) {
        _collectItem = [[SubscriptButton alloc] initWithSize:CGSizeMake(40, 40)];
        [_collectItem.topButton setTitle:@"关注" forState:UIControlStateNormal];
        [_collectItem.topButton imageSetting:@"collect_nor" selectedImage:@"collect_sel"];
        _collectItem.countLabel.hidden = YES;
        _collectItem.userInteractionEnabled = YES;
        _collectItem.topButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [_collectItem.topButton addTarget:self action:@selector(collectGoods) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectItem;
}

- (UIButton *)backBtn {
    
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _backBtn.backgroundColor = [UIColor blackColor];
        [_backBtn setImage:[UIImage imageNamed:@"detail_back"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (UIButton *)paiBtn {
    if (!_paiBtn) {
        _paiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_paiBtn setTitle:@"进入拍场" forState:UIControlStateNormal];
        [_paiBtn addTarget:self action:@selector(goPai) forControlEvents:UIControlEventTouchUpInside];
        [_paiBtn setBackgroundColor:PP_Default_Color_Red_FF3737];
    }
    return _paiBtn;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.backgroundColor = PP_Default_Color_Gray_F1F1F1;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.tag = 1000;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerClass:[DetailBannerCell class] forCellReuseIdentifier:@"DetailBannerCellID"];
        
        [_tableview registerClass:[DetailWebCell class] forCellReuseIdentifier:@"DetailWebCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailCountDownCell" bundle:nil] forCellReuseIdentifier:@"DetailCountDownCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailPriceCell" bundle:nil] forCellReuseIdentifier:@"DetailPriceCellID"];
                
        [_tableview registerNib:[UINib nibWithNibName:@"DetailGoodInfoCell" bundle:nil] forCellReuseIdentifier:@"DetailGoodInfoCellID"];
        //正常的商品信息
        [_tableview registerNib:[UINib nibWithNibName:@"DetailGuaranteeCell" bundle:nil] forCellReuseIdentifier:@"DetailGuaranteeCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailOfferRecordCell" bundle:nil] forCellReuseIdentifier:@"DetailOfferRecordCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailSupplierCell" bundle:nil] forCellReuseIdentifier:@"DetailSupplierCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailBrandCell" bundle:nil] forCellReuseIdentifier:@"DetailBrandCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailOtherStoreCell" bundle:nil] forCellReuseIdentifier:@"DetailOtherStoreCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailOfferRecordCell" bundle:nil] forCellReuseIdentifier:@"DetailOfferRecordCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailActionCell" bundle:nil] forCellReuseIdentifier:@"DetailActionCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"HomeDistanceCell" bundle:nil] forCellReuseIdentifier:@"HomeDistanceCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailImageAndTextCell" bundle:nil] forCellReuseIdentifier:@"DetailImageAndTextCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"DetailImageCell" bundle:nil] forCellReuseIdentifier:@"DetailImageCellID"];

    }
    return _tableview;
}

- (DetailGoodInfoModel *)detailModel {
    if (!_detailModel) {
        _detailModel = [[DetailGoodInfoModel alloc] init];
        _detailModel.itemImageArray = @[].mutableCopy;
    }
    return _detailModel;
}

- (NSMutableDictionary *)heightDic {
    if (!_heightDic) {
        _heightDic = @{}.mutableCopy;
    }
    return _heightDic;
}

@end
